package com.cff.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Properties;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import org.apache.commons.lang3.StringUtils;

import com.cff.common.Constant;
import com.cff.common.SimpleFrame;
import com.cff.util.DateUtil;
import com.cff.util.EventUtil;
import com.cff.util.FileUtil;
import com.cff.util.ISOUtil;
import com.cff.util.SignTools;
import com.cff.util.TelnetTools;
import com.cff.*;

public class SktInfoDownActionListener implements ActionListener {
	private static Properties confPro = null;
	
	public SktInfoDownActionListener() {
		super();
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		confPro = FileUtil.getInstance();
		final JFrame tmpFrame = new JFrame();
		tmpFrame.setBounds(100, 100, 600, 400);
		tmpFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		tmpFrame.setLayout(null);
		tmpFrame.setTitle(confPro.getProperty("tran3title"));
		
		JLabel outUserNoLabel = new JLabel();
		outUserNoLabel.setBounds(20, 20, 80, 50);
		outUserNoLabel.setText("IP:");
		
		JLabel inUserNoLabel = new JLabel();
		inUserNoLabel.setBounds(20, 70, 80, 50);
		inUserNoLabel.setText("端口:");
		
		JLabel actIdLabel = new JLabel();
		actIdLabel.setBounds(20, 120, 80, 50);
		actIdLabel.setText(confPro.getProperty("tran3param1"));
		
		JLabel terIdLabel = new JLabel();
		terIdLabel.setBounds(20, 170, 80, 50);
		terIdLabel.setText(confPro.getProperty("tran3param2"));
		
		JLabel resultLabel = new JLabel();
		resultLabel.setBounds(20, 240, 80, 50);
		resultLabel.setText("执行结果:");
		
		final JTextField outUserNoText = new JTextField();
		outUserNoText.setBounds(100, 35, 180, 20);
		outUserNoText.setText(confPro.getProperty("tran3param3"));
		
		final JTextField inUserNoText = new JTextField();
		inUserNoText.setBounds(100, 85, 180, 20);
		inUserNoText.setText(confPro.getProperty("tran3param4"));
		
		final JTextField actIdText = new JTextField();
		actIdText.setBounds(100, 135, 180, 20);
		actIdText.setText(confPro.getProperty("tran3param5"));
		
		final JTextField terIdText = new JTextField();
		terIdText.setBounds(100, 185, 180, 20);
		terIdText.setText(confPro.getProperty("tran3param6"));
		
		final JTextField resultText = new JTextField();
		resultText.setBounds(100, 255, 380, 20);
	
		JButton excuteButton = new JButton("执行"); 
		excuteButton.setBounds(150, 300, 100, 30);
        JButton cancelButton = new JButton("取消");
        cancelButton.setBounds(300, 300, 100, 30);
        
		tmpFrame.add(actIdLabel);
		tmpFrame.add(terIdLabel);
		tmpFrame.add(inUserNoLabel);
		tmpFrame.add(outUserNoLabel);
		tmpFrame.add(resultLabel);
		tmpFrame.add(outUserNoText);
		tmpFrame.add(inUserNoText);
		tmpFrame.add(resultText);
		tmpFrame.add(actIdText);
		tmpFrame.add(terIdText);
		tmpFrame.add(excuteButton);
		tmpFrame.add(cancelButton);
		
		excuteButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				String outUserNo = outUserNoText.getText().trim();
				String inUserNo = inUserNoText.getText().trim();
				String actId = actIdText.getText().trim();
				String terId = terIdText.getText().trim();
				if(StringUtils.isNotEmpty(actId)){
					actId = appendStr(actId);
				}
				if(StringUtils.isNotEmpty(terId)){
					terId = appendStr(terId);
				}
				if(StringUtils.isEmpty(outUserNo) || StringUtils.isEmpty(inUserNo) || StringUtils.isEmpty(actId)|| StringUtils.isEmpty(terId)){
					JOptionPane.showMessageDialog(null,"数据输入为空！");
					return;
				}
				String result = "";
				try {
					result = sendOnce(outUserNo,inUserNo,actId,terId);
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				resultText.setText(result);
			}

		});
		
		cancelButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				tmpFrame.dispose();
			}
			
		});
		
		tmpFrame.setVisible(true);
		
	}

	
	public static String sendOnce(String ip, String port, String mchntNo, String termNo) throws UnknownHostException, IOException{
		System.out.println(ip);
	
		String str = "600000000060310000000008000020000000C40011100079" + termNo + mchntNo
		+"0051323600183030303030303030303030303030303030303435000132363000123030303030"
		+ "3030303030303036340004303030300011000000053500786C37DE9D181400";
		byte[]  packs = ISOUtil.hex2byte(str);
		byte[] req = new byte[packs.length + 2];
		System.arraycopy(ISOUtil.toHex2Len(packs.length),0, req,0,2);
		System.arraycopy(packs, 0, req, 2, packs.length);
		Socket client = new Socket(ip, 5102);
		client.setSoTimeout(60  * 1000);
		DataInputStream in = new DataInputStream(new BufferedInputStream(
				client.getInputStream()));
		DataOutputStream out = new DataOutputStream(client
				.getOutputStream());
		out.write(req);
		out.flush();
		byte[] header = new byte[2];  //先读取长度位2字节
	    int len = in.read(header);
	    int msgLen = Integer.parseInt(ISOUtil.hexString(header),16);
	    System.out.println("msgLen:" + msgLen);
		byte[] buffer = new byte[msgLen];
		ByteArrayOutputStream bas = new ByteArrayOutputStream();
		int offset = 0;
		while (offset < msgLen) {
			len = in.read(buffer, offset, msgLen - offset);
	        if (len == -1) {
	        	System.out.println("read error");
	        }
	        offset += len;
	        bas.write(buffer);
		}
		byte[] data = bas.toByteArray();
		int length  = data.length;
		System.out.println("res len:" + length);
		client.close();
		return ISOUtil.hexString(data);
	}
	
	public static String appendStr(String str){
		StringBuffer tb = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
        	tb.append("3");
        	tb.append(str.charAt(i));
        }
        return tb.toString();
	}
	
}
